#include "TeacherMyStudentInfo.h"
#include "ui_TeacherMyStudentInfo.h"
#include <QDebug>
#include <QSqlError>
#include <QHeaderView>
#include <QTableWidgetItem>
#include <QLabel>
#include <QPixmap>

#include "teachinterface.h"

TeacherMyStudentInfo::TeacherMyStudentInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TeacherMyStudentInfo)
{
    ui->setupUi(this);

    //对信号进行连接
     connect(ui->tableWidget, &QTableWidget::cellDoubleClicked, this, &TeacherMyStudentInfo::on_tableWidget_cellDoubleClicked);

    setWindowTitle("学生个人界面");

    //连接数据库操作
//    db = QSqlDatabase::addDatabase("QMYSQL"); //添加数据库
//    db.setHostName("192.168.175.131"); //数据库IP地址
//    db.setUserName("root"); // 数据库用户名
//    db.setPassword("123456"); //数据库密码
//    db.setDatabaseName("test268"); // 数据库表名
//    db.setPort(3306);

//    //判断打开数据库是否为空，为空打印错误信息
//    if(!db.open())
//    {
//        qDebug() << db.lastError() << endl;
//        return ;
//    }

    //mysql语句执行对象
    QSqlQuery query;

    //判断查询StudentInformation是否成功，不成功打印信息
    if(query.exec("select *from student_list;") == false)
    {
        //qDebug() << db.lastError() << endl;
        return;
    }

    //设置表头
    QStringList headers;
    headers << "班级" << "学号" << "姓名" << "性别" << " 成绩" << "指导老师";
    //设置列数
    ui->tableWidget->setColumnCount(6);
    //设置水平方向
    ui->tableWidget->setHorizontalHeaderLabels(headers);
    // 隐藏行号
    ui->tableWidget->verticalHeader()->setVisible(false);

    //设置在第几行插入数据
    ui->tableWidget->setRowCount(0);
    int currentRow = 0;

    while( query.next() == true) //从结果集中读取下一行数据
    {
        QTableWidgetItem *ClassItem = new QTableWidgetItem(query.value("class").toString());
        QTableWidgetItem *IDItem = new QTableWidgetItem(query.value("id").toString());
        QTableWidgetItem *NameItem = new QTableWidgetItem(query.value("Name").toString());
        QTableWidgetItem *SexItem = new QTableWidgetItem(query.value("Sex").toString());
        // 判断分数是否为0
        QString scoreValue = query.value("Score").toString();

        if (scoreValue == "0")
        {
            scoreValue = ""; // 如果为0，则设置为空字符串
        }

        QTableWidgetItem *ScoreItem = new QTableWidgetItem(query.value("Score").toString());
        QTableWidgetItem *TeacherItem = new QTableWidgetItem(query.value("Teacher").toString());

        // 设置文本居中对齐
        ClassItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        IDItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        NameItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        SexItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        ScoreItem->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
        TeacherItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

        ui->tableWidget->insertRow(currentRow);
        ui->tableWidget->setItem(currentRow, 0, ClassItem);
        ui->tableWidget->setItem(currentRow, 1, IDItem);
        ui->tableWidget->setItem(currentRow, 2, NameItem);
        ui->tableWidget->setItem(currentRow, 3, SexItem);
        ui->tableWidget->setItem(currentRow, 4, ScoreItem);
        ui->tableWidget->setItem(currentRow, 5, TeacherItem);
        currentRow++;
    }

    //表头拉伸
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    //连接
    //修改按钮连接
    connect(ui->Update_Pushbutton, &QPushButton::clicked, this, &TeacherMyStudentInfo::UpdateData);

    //laber中添加图片
    ui->label->setPixmap(QPixmap(":::/TeacherImage/stu.png"));
    ui->label->setScaledContents(true);

    //设置背景图片
    this->setStyleSheet("background-image: url(:/TeacherImage/bk.jpg);");

}

TeacherMyStudentInfo::~TeacherMyStudentInfo()
{
    delete ui;
}


//修改数据
void TeacherMyStudentInfo::UpdateData()
{
    // 检查是否有选中的行
    int row = ui->tableWidget->currentRow();

    if (row == -1)
    {
        qDebug() << "请选择一行来修改成绩";
        return;
    }

    // 获取当前选中行的学号
    QString studentID = ui->tableWidget->item(row, 1)->text();

    // 获取成绩单元格，并使其可编辑
    QTableWidgetItem *scoreItem = ui->tableWidget->item(row, 4);

    if (!scoreItem)
    {
        return;
    }

    ui->tableWidget->editItem(scoreItem); // 启用编辑模式

    // 捕获编辑完成后的分数
    connect(ui->tableWidget, &QTableWidget::itemChanged, [ = ](QTableWidgetItem * item)
    {
        if (item->column() == 4 && item == scoreItem)
        {
            // 确保修改的是成绩单元格
            QString newScore = item->text();

            // 更新数据库
            QSqlQuery query;
            query.prepare("update StudentInformation set Score = :score where Id = :id");
            query.bindValue(":score", newScore);
            query.bindValue(":id", studentID);

            if (query.exec())
            {
                qDebug() << "成绩更新成功";
            }
            else
            {
                qDebug() << "成绩更新失败: " << query.lastError();
            }
        }
    });
}

void TeacherMyStudentInfo::painterEvent(QPaintEvent *e)
{
    //定义绘画目标
    QPainter paint(this);
    QPixmap image(":/TeacherImage/bk.jpg");
    paint.drawPixmap(0, 0, this->width(), this->height(), image);
}



void TeacherMyStudentInfo::on_tableWidget_cellDoubleClicked(int row, int column)
{
    // 检查是否是“姓名”列（假设姓名列是第二列，索引从0开始）
        if (column == 2)  // 这里假设姓名列的索引是2
        {
            // 获取学生姓名
            QString studentName = ui->tableWidget->item(row, 2)->text();
            teachInterface *info_view=new teachInterface;
            info_view->show();
        }


}
